package Gui.Dialogs;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import Cards.Card;
import Cards.EventCardLoss;
import Main.Defines;

public class EventDayDialog extends JDialog {
	private JLabel TitleLabel = null;
	private JLabel ValueLabel = null;
	private JButton buttonOK = null;
	private Integer value = 0;
	private Integer cost = 0;

	private void buildUIContents(final Card theEvent){
		TitleLabel = new JLabel(theEvent.getName());

		value = theEvent.getValue();

		if (theEvent.getM_cardType() == Card.EventCardLoss) {
			cost = ((EventCardLoss) theEvent).getCost();
			ValueLabel = new JLabel("You pay " + cost.toString());
		} else {
			ValueLabel = new JLabel("You receive " + value.toString());
		}

		buttonOK = new JButton("Ok");
		buttonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Defines.CP().AddCash(value-cost);
				while ( Defines.CP().getCash() < 0 ){
					Defines.CP().AddCash(1000);
					Defines.CP().AddLoan(1000);
				}
				dispose();
			}
		});
		add(TitleLabel);
		add(ValueLabel);
		add(buttonOK);
	}
	public EventDayDialog(JFrame parent, final Card theEvent) {
		super(parent, "Event", true);
		setLayout(new GridLayout(3, 1));
		setLocationRelativeTo(parent);
		buildUIContents(theEvent);
		
		pack();
		// REFACTOR THIS! MAGIC NUMBER
		if(getTitle() != null){
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + 75; // MAGIC NUMBER, 75 is size of a "typical" window icon
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}

		setVisible(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
}